<?php namespace Home\Controller; 
use Hdphp\Controller\Controller;
class ContentController extends Controller{
	public function index(){
		$gid=Q('get.gid',0,'intval');
//		P($gid);die;
		//获取内容页的数据
		$goodsModel=new \Common\Model\Goods;
		$goodsData=$goodsModel->where(array('gid'=>$gid))->find();
		$goodsDetail=new \Common\Model\Detail;
		$detailData=$goodsDetail->where(array('gid'=>$gid))->find();
		$detailData['big']=explode(',', $detailData['big']);
		$detailData['small']=explode(',', $detailData['small']);
		$detailData['mid']=explode(',', $detailData['mid']);
//		p($detailData);
//		p($goodsData);
		View::with('goodsData',$goodsData);
		View::with('detailData',$detailData);
		//商品属性表
		$goodsAttr= new \Common\Model\GoodsAttr;
		$attrData=$goodsAttr->join('type_attr','beautiful_goods_attr.taid','=','beautiful_type_attr.taid')->where(array('gid'=>$gid))->where('class=1')->get();
		//重组数组将$attrData组成希望的数组；包含规格和附加价格
		$size=array();
		foreach ($attrData as $k => $v) {
			$size[$v['taname']][]=$v['gtvalue'].','.$v['added'];
		}
		$listModel=new \Common\Model\GoodsList;
		$listData=$listModel->where(array('gid'=>$gid))->get();
		foreach ($size as $key => $value) {
			foreach($value as $k=>$v){
				$size[$key][$k]=explode(',', $v);
			}

		}
		p($size);
//		p($attrData);
		View::with('size',$size);
		View::make();
	}
	public function AjaxGet(){
//		p($_POST);
		$listModel=new \Common\Model\GoodsList;
		$goodsAttr=new \Common\Model\GoodsAttr;
		//处理提交过来的数据
		//统计$_POST['spec']的数组长度
		$len=count($_POST['spec']);
		//当只点击了一个时:
		if($len==1){
			//根据商品主键gid以及发送过来的规格值，知道到本规格所对应的主键gtid
			$gtid=$goodsAttr->where(array('gid'=>$_POST['gid']))->where(array('gtvalue'=>$_POST['spec'][0]))->pluck('gtid');
		
		//获取本商品的货品的数据
		$data=$listModel->where(array('gid'=>$_POST['gid']))->get();
		//循环$data,将组合ID 'combine'转化为数组形式
		foreach ($data as $key => $value) {
			$data[$key]['combine']=explode(',', $value['combine']);
		}
		//循环遍历新的data数组，找出所获得的gtid是否在combine这个数组中，如果在，获取库存量
		$kucun=0;
		foreach ($data as $key => $value) {
			if(in_array($gtid,$value['combine'])){
				$kucun += $value['inventory'];
			}
		}
//		p($_POST['gid']);
//		p($gtid);
//		p($kucun);
		View::ajax($kucun);
	}
		//如果获取了两个规格时:
		if($len==2){
			//遍历$_POST['spec'],获取规格对应的gtid
			$gtid=array();
			foreach ($_POST['spec'] as $key => $value) {
				$id=$goodsAttr->where(array('gid'=>$_POST['gid']))->where(array('gtvalue'=>$value))->pluck('gtid');
				$gtid[]=$id;
			}
		
		//将数组形式的$gtid转化为字符串
		$gtid=implode(',', $gtid);
		//获取所属商品且字段combine的数据为$gtid的库存
		$data=$listModel->where(array('gid'=>$_POST['gid']))->where(array('combine'=>$gtid))->pluck('inventory');
//		p($gtid);
//		p($data);
		View::ajax($data);
		}
		
	}
	public function shopCar(){
//		p($_POST);
		$goodsAttr=new \Common\Model\GoodsAttr;
		$listModel=new \Common\Model\GoodsList;
		//获取货品的ID
		$arr=array();
		$arr[]=$_POST['mes'];
		$arr[]=$_POST['meg'];
//		p($arr);die;
		//遍历$_POST['spec'],获取规格对应的gtid
		$gtid=array();
		foreach ($arr as $key => $value) {
			$id=$goodsAttr->where(array('gid'=>$_POST['gid']))->where(array('gtvalue'=>$value))->pluck('gtid');
			$gtid[]=$id;
		}
		
		//将数组形式的$gtid转化为字符串
		$gtid=implode(',', $gtid);
		$listID=$listModel->where(array('combine'=>$gtid))->pluck('glid');
		$nus=$listModel->where(array('combine'=>$gtid))->pluck('number');
//		p($listID);
			$data = array( 
			    'id'        => $_POST['gid'], // 商品 ID 
			    'name'      => $_POST['gname'],// 商品名称 
			    'num'       => $_POST['number'], // 商品数量 
			    'price'     => $_POST['shopprice'], // 商品价格 
			    'options'   => array(
									'bid'=>$_POST['bid'],
									'marketprice'=>$_POST['marketprice'],
									'color'     => $_POST['mes'] ,
							    	'size'  =>$_POST['meg'] ,
							    	'pic'=>$_POST['pic'],
							    	'listID'=>$listID,
							    	'nus'=>$nus
				),
			   
		); 
		Cart::add($data); // 添加到购物车 
//		p($_SESSION); 
		//

	}
}
?>